<?php
/**
 *
 * cp3.15活动 优惠券发放日志 model
 * powered by jeff liu 2012-03-01
 * @author jeff
 *
 */
class Cp315_send_coupon_log_single_userApiModel extends BaseModel
{

    var $table = 'cp315_send_coupon_log';

    function log_send_coupon_result($coupon_sn_info, $code, $msg, $description)
    {
        if (is_array($coupon_sn_info)) {
            foreach ($coupon_sn_info as $user_id => $coupon_sns) {
                $sql = sprintf(
                    'INSERT INTO %s(`user_id`, `coupon_sn`, `code`, `msg`, `description`, `add_time`)VALUES',
                    $this->table
                );
                $tmp_sql = array();
                if (is_array($coupon_sns)) {

                    foreach ($coupon_sns as $coupon_sn) {
                        $tmp_sql[] = sprintf(
                            '("%.0f", "%s", %.0f, "%s", "%s", %.0f)',
                            (float)$user_id,
                            $coupon_sn,
                            $code,
                            $msg,
                            $description,
                            gmtime()
                        );
                    }
                } else {
                    $tmp_sql[] = sprintf(
                        '("%.0f", "%s", %.0f, "%s", "%s", %.0f)',
                        (float)$user_id,
                        $coupon_sns,
                        (float)$code,
                        $msg,
                        $description,
                        gmtime()
                    );
                }

                $sql .= implode(',', $tmp_sql) . ';';

                $this->db->query($sql);
            }
        }
    }


    function escape_duplicate_user_id($user_id)
    {
        $final_user_id = array();
        $condition_user_id = null;
        if (is_array($user_id)) {
            $tmp_user_id = array();
            foreach ($user_id as $id) {
                if (!empty($id)) {
                    $tmp_user_id[] = sprintf('%.0f', (float)$id);
                }
            }
            $condition_user_id = array_unique($tmp_user_id);
        } else {
            if (is_string($user_id)) {
                $tmp_user_id = sprintf('%.0f', (float)$user_id);
                $condition_user_id = array($tmp_user_id);
            }
        }

        if (!empty($condition_user_id)) {
            if (count($condition_user_id) > 1) {
                $sql = sprintf(
                    'SELECT `user_id` FROM %s WHERE `user_id` IN (%s) AND `code` = 1',
                    $this->table,
                    implode(',', $condition_user_id)
                );
            } else {
                $id = $condition_user_id[0];
                $sql = sprintf(
                    'SELECT `user_id` FROM %s WHERE `user_id` = %.0f  AND `code` = 1',
                    $this->table,
                    (float)$id
                );
            }
            $rs = $this->getAll($sql);

            if (!empty($rs)) {
                foreach ($rs as $r) {
                    $final_user_id[] = $r['user_id'];
                }
            }
        }

        return $final_user_id;
    }
}